const express = require("express");
const router = express.Router();
const pool = require("../pool");
const nodemail = require("../utils/mailserver");

let tools = {
  createSixNum() {
    var Num = "";
    for (var i = 0; i < 6; i++) {
      Num += Math.floor(Math.random() * 10);
    }
    return Num;
  },
};

let userNameArr = [
  "WearMask",
  "风花雪月",
  "十月围城",
  "革命仍未成功",
  "十月革命",
  "晋南风采",
];

router.get("/email", async (req, res, next) => {
  console.log(req.query);
  var email = req.query.email;
  var code = await tools.createSixNum();
  var date = new Date();
  var isLive = "no";
  console.log(email);
  let mail = {
    from: "980142138@qq.com",

    subject: "张静邮箱测试",

    to: email,

    text: "用" + code + "作为你的验证码,服务端邮箱测试",
  };

  pool.query("select u_id from user where email=?", [email], async (err, r) => {
    if (err) {
      return next(err);
    }
    if (r.length > 0) {
      pool.query(
        "select u_id from user where nickname is null",
        (err, resq) => {
          if (err) {
            return next(err);
          }
          if (resq.length > 0) {
            let id = resq[0]["u_id"];
            pool.query(
              `update user set code=? where u_id=${id}`,
              [code],
              (err, r) => {
                if (err) {
                  return next(err);
                }
                res.send({ success: true, message: "邮箱可注册" });
                nodemail(mail);
              }
            );
          } else {
            res.send({ success: false, message: "邮箱已注册" });
          }
        }
      );

      return;
    } else {
      pool.query(
        "insert into user values(null,?,?,default,default,?,default,?,default,?,?)",
        [null, null, date, email, code, isLive],
        (err, r) => {
          if (err) {
            return next(err);
          }
          res.send({ success: true, message: "邮箱可注册" });
        }
      );
      nodemail(mail);
    }
  });
});

router.post("/doRegister", async (req, res, next) => {
  var username =
    userNameArr[Math.floor(Math.random() * (userNameArr.length - 1))];
  var password = req.body.password;
  var email = req.body.email;
  var code = req.body.code;
  console.log(code);
  var nowDate = new Date().getTime();
  pool.query(
    "select * from user  where nickname =?",
    [username],
    (err, resq) => {
      if (resq.length > 0) {
        res.send({ success: false, message: "用户已注册" });
      } else {
        pool.query("select * from user where email=?", [email], (err, r) => {
          if (err) {
            return next(err);
          }
          // console.log( r[0].code)
          if (
            r[0].code === code &&
            r[0].birthday.getTime() - nowDate < 600000
          ) {
            pool.query(
              "update user set nickname=?,pwd=?,birthday=?,email=?,isLive=? where email = ?",
              [username, password, r[0].birthday, email, "yes", email]
            );
            res.send({ success: true, message: "注册成功" });
          } else {
            res.send({ success: false, message: "验证码过期" });
          }
        });
      }
    }
  );
});

module.exports = router;
